public without sharing class EquipmentController {

	public String backId {get;private set;}
	public String msg {get;private set;}
	public Boolean isSuccess {get;private set;}

	public Boolean isErr {get;private set;}
	public String dataJson {get;private set;}
	public String saveJson {get; set;}
	private Map<String,DataUtil.SObjectBean> stageObjectMap;

    public EquipmentController() {
        
    	backId = System.currentPageReference().getParameters().get('id');
      	stageObjectMap = new  Map<String,DataUtil.SObjectBean>();

    }


    public void init(){

    	try {
    	    
    	    if(backId == null ) throw new DmlException('页面打开错误!');

    	    Equipment__c eq = [select Id ,ConfirmWarranty__c from Equipment__c where Id=:backId];
    	    if(eq.ConfirmWarranty__c )throw new DmlException('已确认保修条款,不需重复确认!');

    	    Map<String,Object> dataMap = new Map<String,Object>();

             //字段
             Map<String,Set<String>> fieldsmap = getFields();
             //字段详情
             Map<String, List<DataUtil.FieldBean>> fbMap = DataUtil.getFieldsBean(fieldsmap,stageObjectMap);
            
            //字段赋值
             Map<String,List<DataUtil.FieldBean>> fields = DataUtil.setFieldsValue(fbMap,DataUtil.getBaseInfo(fbMap,stageObjectMap) );
           
            dataMap.put('base',fields);
            dataMap.put('baseObject',DataUtil.getBaseObjectMap(stageObjectMap)); 


           

        	dataJson = EncodingUtil.urlEncode(JSON.serialize(dataMap ),'UTF-8');
    	}
    	catch (Exception e) {
    	    isErr =true;
    		ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR ,e.getMessage()));

    	}
    	
    }

     public Map<String,Set<String>>  getFields(){
        Map<String,Set<String>> fieldsMap  = new  Map<String,Set<String>>();
        
            DataUtil.SObjectBean sb = new DataUtil.SObjectBean();

            sb.title ='';
            sb.objname = 'Equipment__c';
            sb.stype = DataUtil.getSObjectType(sb.objname);
            sb.idvalue = backId;
            sb.type = 'base';
            sb.condition = ' where Id =\''+backId +'\'';
            stageObjectMap.put(sb.objname,sb );
            //'InvoiceAccount__c',
            Set<String>  fields = new Set<String>{
            	'WarrantyPeriod__c','ExpiryDate__c',
            	'ResponseHours__c','Warranty__c',
            	'ApplicationTraining__c','ChargingStandard__c',
            	'EquipmentCheck__c','Period__c',
            	'EquipmentCheckGoods__c','RapidWear__c',
            	'CheckPeriod__c','CommonAccessories__c',
            	'SpecialRequirement__c'
        											};
         
            fieldsMap.put(sb.objname, fields);
        
        return fieldsMap;
    }

    public void save (){
    	msg = '';
   		try {

		    Map<String,Object>saveMap = (Map<String,Object>) JSON.deserializeUntyped(EncodingUtil.urlDecode(saveJson, 'UTF-8'));

		     Map<String,Set<String>> fieldsMap = getFields();
        	Map<String,DataUtil.FieldBean> fbeanMap = DataUtil.getFieldsInfo(Equipment__c.SObjecttype,fieldsMap.get('Equipment__c'));

        	Equipment__c eq = new Equipment__c(Id=backId);
            Map<String,Object> obj = (Map<String,Object>) saveMap.get('base');

            for (String field :  obj.keySet()) {

                if(obj.get(field) == null || DataUtil.getStringValue(obj.get(field)) == '') continue;

                if(fbeanMap.containsKey(field) )eq.put(field, DataUtil.dataHandler(obj.get(field),fbeanMap.get(field).type.toUpperCase() ));

            }
            eq.ConfirmWarranty__c = true;
            update eq;
            isSuccess = true;
            msg = '保存成功!';
        }catch(Exception e){
        	isSuccess = false;
        	msg = '保存失败:'+e.getMessage();

        }
    }

}